function bianhao() {
    $.getJSON("/car/selCarGuanli",function (data) {
        console.log(data)
        $(data).each(function (i,v) {
            $(".bianhao").append("<option value='"+v.id+"'>"+v.model+"</option>")
        })
    })
}


function saveYongChu() {
    let bianhao = $(".bianhao").val();
    let yongcheDate = $(".yongcheDate").val();
    let yongcheshiyou = $(".yongcheshiyou").val();
    let shenpi = $(".shenpi").val();
    var now = new Date();
    var day = ("0" + now.getDate()).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var date1 = now.getFullYear()+"-"+(month)+"-"+(day) ;

    if (bianhao==-1){
        layer.tips('请选择车辆', '.bianhao');
        return
    }
    if (yongcheDate==""){
        layer.tips('请选择时间', '.yongcheDate');
        return;
    }
    if (yongcheDate<date1){
        layer.tips('请选择正确时间', '.yongcheDate', {
            tips: 4
        });
        return;
    }
    if (yongcheshiyou==""){
        layer.tips('请输入用车事由', '.yongcheshiyou');
        return;
    }
    if (shenpi==-1){
        layer.tips('请选择审批人', '.shenpi', {
            tips: 3
        });
        return;
    }

    let json={
        ci_carid:bianhao,
        usetime:yongcheDate,
        reason:yongcheshiyou,
        approver:shenpi
    }
    $.getJSON("/car/yongche",json,function (data) {
        if (data){
            layer.alert('操作成功', {icon: 6});
        }else{
            layer.alert('操作失败', {icon: 5});
        }
        setTimeout("window.location.href='/to/yongche'",1000);
    })
}

/*车辆信息管理*/

function showCar() {
    $.getJSON("/car/showCar",function (data) {
        if (data==""){
            $(".car-table").append("<tr><td colspan='5'><h2 style='text-align: center'>暂无车辆，请添加</h2></td></tr>");
        }
        $(data).each(function (i,v) {
            $(".car-table").append("<tr><td>"+v.car+"</td><td>"+v.model+"</td><td>"+v.number+"</td><td>"+v.comment+"</td><td><button  class='layui-btn layui-btn-sm' onclick='XiuGaitanchuang("+v.id+")'>修改</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class='layui-btn layui-btn-sm' onclick='deteCar("+v.id+",this)'>删除</button></td></tr>")
        })
    })

}

function deteCar(id,thisA) {
    let bo=false;
    layer.msg('你确定要删除吗？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            layer.close(index);
            let json={
                id:id
            }
            $.getJSON("/car/shanchu",json,function (data) {
                if (data){
                    layer.alert('删除成功', {icon: 6});
                    $(thisA).parent("tr").remove();
                }else{
                    layer.alert('删除失败', {icon: 5});
                }
                setTimeout("window.location.href='/to/cheLiang'",1000);

            })
        }
    });


}
var bo =false ;
function panduan() {
    let bainhao = $(".bainhao").val();
    let json={
        number:bainhao
    }
    $.getJSON("/car/panduan",json,function (data) {
        bo = data;
        if (data){
            layer.tips('车辆编号已存在，请更换', '.bainhao');
        }
    })
}
//下拉框点击后弹层抓值到后台做修改操作
function tanchuang() {
    layer.open({
        title: '添加车辆信息',
        area: ['500px', '400px'],
        btn:["确定","取消"],
        content: '<table class="layui-table">\n' +
            '        <tr>\n' +
            '            <td>车牌号：</td>\n' +
            '            <td><input type="text" class="layui-input chepahao"></td>\n' +
            '        </tr>\n' +
            '        <tr>\n' +
            '            <td>车型：</td>\n' +
            '            <td><input type="text" class="layui-input chetype"></td>\n' +
            '        </tr>\n' +
            '        <tr>\n' +
            '            <td>编号：</td>\n' +
            '            <td><input type="text" class="layui-input bainhao" onchange="panduan()"></td>\n' +
            '        </tr>\n' +
            '        <tr>\n' +
            '            <td>备注：</td>\n' +
            '            <td><input type="text" class="layui-input beizhu"></td>\n' +
            '        </tr>\n' +
            '    </table>',

        success:function(){//弹出层弹出后回调
        },
        btn1:function() {//确定事件
            let chepahao = $(".chepahao").val();
            let chetype = $(".chetype").val();
            let bainhao = $(".bainhao").val();
            let beizhu = $(".beizhu").val();
            if(chepahao==""){
                layer.tips('车牌号不能为空', '.chepahao');
                return
            }
            if(chetype==""){
                layer.tips('车型不能为空', '.chetype');
                return
            }
            if(bainhao==""){
                layer.tips('车辆编号不能为空', '.bainhao');
                return
            }
            if(beizhu==""){
                layer.tips('备注不能为空', '.beizhu');
                return
            }
            panduan();
            if (bo){
                return;
            }
            let json ={
                car:chepahao,
                model:chetype,
                number:bainhao,
                comment:beizhu
            }
            $.getJSON("/car/saveCar",json,function (data) {
                if (data){
                    layer.alert('添加成功', {icon: 6});
                }else{
                    layer.alert('添加失败', {icon: 5});
                }
                setTimeout("window.location.href='/to/cheLiang'",1000);
            })
        },
        btn2:function(){//取消事件

        }
    });
}
//下拉框点击后弹层抓值到后台做修改操作
function XiuGaitanchuang(id) {
    let id1 = id;
    let json={
        id:id1
    }
    $.getJSON("/car/selCarID",json,function (data) {
        console.log(data)
        layer.open({
            title: '修改车辆信息',
            content: '<table class="layui-table">\n' +
                '        <tr>\n' +
                '            <td>车牌号：</td>\n' +
                '            <td><input type="text" class="layui-input chepahao" value="'+data.car+'"></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>车型：</td>\n' +
                '            <td><input type="text" class="layui-input chetype" value="'+data.model+'"></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>编号：</td>\n' +
                '            <td><input type="text" class="layui-input bainhao" value="'+data.number+'" onchange="panduan()"></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>备注：</td>\n' +
                '            <td><input type="text" class="layui-input beizhu" value="'+data.comment+'"></td>\n' +
                '        </tr>\n' +
                '    </table>',
            area: ['500px', '400px'],
            btn:["确定","取消"],
            success:function(){//弹出层弹出后回调
            },
            btn1:function() {//确定事件
                let chepahao = $(".chepahao").val();
                let chetype = $(".chetype").val();
                let bainhao = $(".bainhao").val();
                let beizhu = $(".beizhu").val();
                if(chepahao==""){
                    layer.tips('车牌号不能为空', '.chepahao');
                    return
                }
                if(chetype==""){
                    layer.tips('车型不能为空', '.chetype');
                    return
                }
                if(bainhao==""){
                    layer.tips('车辆编号不能为空', '.bainhao');
                    return
                }
                if(beizhu==""){
                    layer.tips('备注不能为空', '.beizhu');
                    return
                }
                let json ={
                    id:id1,
                    car:chepahao,
                    model:chetype,
                    number:bainhao,
                    comment:beizhu
                }
                $.getJSON("/car/updaCar",json,function (data) {
                    if (data){
                        layer.alert('修改成功', {icon: 6});
                    }else{
                        layer.alert('修改失败', {icon: 5});
                    }
                    setTimeout("window.location.href='/to/cheLiang'",1000);
                })
            },
            btn2:function(){//取消事件
            }
        });
    })

}

var currentPage = 1;//当前页码
var pageSize = 10;//每页显示的数据量
var totalCount = 0;//总条数
var totalPage = 1;//总页码
/*用车记录*/
function yongchexq() {
    let json = {
        currentPage:currentPage,
        pageSize:pageSize
    }
    $(".yongche-table tr:gt(0) td").html("&nbsp;");
    $.ajaxSettings.async = false;
    $.getJSON("/car/yongchexq",json,function (data) {
        let uname = data.user.name;
        let zhiw = data.jobtitle.job;
        totalCount=data.ch.total;
        totalPage=data.ch.pages;
        console.log(data)
        if (data.ch.list==""){
            $(".yongchetishi").text("暂无用车记录");
        }
        $(data.ch.list).each(function (i,v) {
            let zhutai=""
            if (v.state==0){
                zhutai="审核中"
            }else if(v.state==1){
                zhutai="通过"
            }else if(v.state==-1){
                zhutai="拒绝"
            }else if(v.state==-2){
                zhutai="自己取消"
            }
            //$(".yongche-table").append("<tr><td>"+(i+1)+"</td><td>"+uname+"</td>
            // <td>"+zhiw+"</td><td>"+v.usetime+"</td><td>"+v.reason+"</td>
            // <td>"+v.approver+"</td><td>"+zhutai+"</td>
            // <td><button class='layui-btn' onclick='quxiao2("+v.state+","+v.id+")'>取消</button></td></tr>")
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(0).text((currentPage - 1) * pageSize + i + 1);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(1).text(uname);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(2).text(zhiw);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(3).text(v.usetime);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(4).text(v.reason);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(5).text(v.approver);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(6).text(zhutai);
            $(".yongche-table").find("tr").eq(i+2).find("td").eq(7).html("<button class='layui-btn layui-btn-xs' onclick='quxiao2("+v.state+","+v.id+")'>&nbsp;&nbsp;取&nbsp;消&nbsp;&nbsp;</button>");
        })
    })
    $.ajaxSettings.async = true;
    showPage()
}
function showPage(){
    $(".showPage").text("第" + currentPage + "页 / 共" + totalPage + "页");
}
function next(){
    currentPage++;
    if(currentPage>totalPage){
        currentPage=totalPage;
        return;
    }
    yongchexq()
}
function prev(){
    currentPage--;
    if(currentPage<1){
        currentPage=1;
        return;
    }
    yongchexq()

}
function start(){
    currentPage=1;
    yongchexq()
}
function end(){
    currentPage=totalPage;
    yongchexq()
}
function quxiao2(num,id) {
    if (num==0){
        layer.msg('你确定要取消吗？', {
            time: 0 //不自动关闭
            ,btn: ['确定', '取消']
            ,yes: function(index){
                layer.close(index);
                $.getJSON("/car/updCa","id="+id,function (data) {
                    if (data){
                        layer.alert('操作成功', {icon: 6});
                    }else {
                        layer.alert('操作失败', {icon: 5});
                    }
                    window.location.href='/to/Yongche'
                })
            }
        });


    }else if(num==1){
        layer.alert('已通过，不可取消', {icon: 6});
        return
    }else if(num==-1){
        layer.alert('已拒绝，不可取消', {icon: 5});
        return
    }else if(num==-2){
        layer.alert('已取消', {icon: 5});
        return
    }
}

